package com.cloudcross.ssp.web.back.main;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.Account;
import com.cloudcross.ssp.model.SysMail;
import com.cloudcross.ssp.model.SysMailRead;
import com.cloudcross.ssp.service.ISysMailService;

@Controller
@RequestMapping("/back/main/sysmail")
public class SysMailController extends SimpleController{
	@Autowired
	private ISysMailService sysMailService;
	
	@RequestMapping("/list")
	public String querySysMailByAccountId(Model model,Long accountId,@RequestParam(defaultValue = "1") int page){
		List<SysMail> sysMailList = new ArrayList<SysMail>();
		
		int count = this.sysMailService.querySysMailCountByAccountId(accountId, 1);
		Pager pager =new Pager();
		pager.setPage(page);
		pager.setTotalRow(count);
		
		sysMailList = this.sysMailService.querySysMailByAccountId(accountId, 1, pager);
		model.addAttribute("pager", pager);
		model.addAttribute("sysMailList", sysMailList);
		return page("sysmail");
	}
	
	@RequestMapping("/querySysMailUnRead")
	@ResponseBody
	public int querySysMailUnRead(Long accountId){
		return this.sysMailService.querySysMailUnRead(accountId, 1);
	}
	
	
	@RequestMapping("/addSysMailRead")
	public boolean insertSysMailRead(Long sysMailId){
		Account account = getLoginUser();
		SysMailRead sysMailRead = new SysMailRead();
		sysMailRead.setAccountId(account.getId());
		sysMailRead.setSysMailId(sysMailId);
		return this.sysMailService.insertSysMailRead(sysMailRead);
	}
	
	@RequestMapping("/addSysMailReadBetch")
	public boolean insertSysMailReadBetch(List<Long>sysMaiIdList){
		Account account = getLoginUser();
		return this.sysMailService.insertSysMailReadBetch(sysMaiIdList,account.getId());
	}

}
